import { defineStore } from 'pinia';
import { computed, ref } from 'vue';

function initState() {
  return {
    isCollapse: false,
    tags: [
      {
        name: 'home',
        path: '/home',
        title: '首页',
      },
    ],
    currentTag: null,
  };
}

export const useHomeDataStore = defineStore('homeData', () => {
  // ref 类似于 state属性
  const state = ref(initState());
  function selectMenu(val) {
    if (val.name === 'home') {
      state.value.currentTag = null;
    } else {
      let index = state.value.tags.findIndex((item) => item.name === val.name);
      index === -1 ? state.value.tags.push(val) : '';
    }
  }
  function updateMenu(tags) {
    let index = state.value.tags.findIndex((item) => item.name === tags.name);
    if (index !== -1) {
      state.value.tags.splice(index, 1); // 删除标签
    }
  }

  return {
    state, // 直接暴露整个 state
    selectMenu,
    updateMenu,
  };
});
